<template>
  <div class="login_container">
    <div class="login_box">
      <el-form
        :model="loginForm"
        status-icon
        :rules="rules"
        ref="loginForm"
        label-width="60px"
        class="loginForm"
      >
        <el-form-item label="用户" prop="user">
          <el-input v-model="loginForm.user"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 75px">
          <el-button type="primary" @click="submitForm('loginForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import * as types from "../store/mutation-types";

export default {
  data() {
    var validateUser = (rule, value, callback) => {
      if (value !== "root") {
        callback(new Error("请输入正确的用户"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value !== "root") {
        callback(new Error("请输入正确的密码"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        password: "",
        user: "",
      },
      rules: {
        user: [{ required: true, validator: validateUser, trigger: "blur" }],
        password: [
          { required: true, validator: validatePass, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log("login success!");
        } else {
          console.log("error submit!!");
          return false;
        }
        this.$store.commit(types.SET_USER_TOKEN, this.loginForm.user);
        this.$router.push("/video");
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style scoped>
.login_container {
  background-image: linear-gradient(-180deg, #1a1454 0%, #0e81a5 100%);
  background-repeat: no-repeat;
  background-size: cover;
  height: 100%;
  position: relative;
}
.login_box {
  border-radius: 5px;
  width: 360px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
/* .el-form {
  padding: 32px;
  position: absolute;
  bottom: 0;
  width: 100%;
  box-sizing: border-box;
} */
</style>